package com.kds.smarthome.controller;

import com.kds.smarthome.annotation.BuildingCode;
import com.kds.smarthome.annotation.Log;
import com.kds.smarthome.common.AjaxResponse;
import com.kds.smarthome.controller.vo.OutdoorEnvironment;
import com.kds.smarthome.entity.Building;
import com.kds.smarthome.security.jwt.TokenProvider;
import com.kds.smarthome.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/api/weather")
public class WeatherController {

    @Autowired
    private WeatherService weatherService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private TokenProvider tokenProvider;

    @Log(title = "查询当前天气", businessType = 0)
    @GetMapping("/current")
    public AjaxResponse<OutdoorEnvironment> outdoorEnvironment(@BuildingCode String buildingCode,
                                                               @RequestHeader("Authorization") String token) {
        try {
            Building currentBuilding = tokenProvider.getCurrentBuilding(token, buildingCode);
            if (currentBuilding == null) {
                return AjaxResponse.error("未找到指定建筑");
            }

            log.info("查询location={}", currentBuilding.getLocation());
            // 使用building的location获取天气数据
            OutdoorEnvironment weather = weatherService.getRecentWeather(currentBuilding.getLocation());
            if (weather == null) {
                return AjaxResponse.error("未找到天气数据");
            }

            // 设置建筑名称作为区域名
            weather.setRegion(currentBuilding.getLocationName());

            return AjaxResponse.success(weather);
        } catch (RuntimeException e) {
            return AjaxResponse.error(e.getMessage());
        }
    }



    @Log(title = "查询历史天气", businessType = 0)
    @GetMapping("/history")
    public AjaxResponse<List<Map<String, Object>>> getHistoryWeather(
            @BuildingCode String buildingCode,
            @RequestHeader("Authorization") String token,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate) {

        try {
            Building currentBuilding = tokenProvider.getCurrentBuilding(token, buildingCode);
            if (currentBuilding == null) {
                return AjaxResponse.error("未找到指定建筑");
            }

            log.info("查询历史天气数据: location={}, start={}, end={}",
                currentBuilding.getLocation(), startDate, endDate);

            List<Map<String, Object>> historyData = weatherService.getHistoryWeather(
                currentBuilding.getLocation(), startDate, endDate);

            return AjaxResponse.success(historyData);
        } catch (RuntimeException e) {
            return AjaxResponse.error(e.getMessage());
        }
    }
}
